﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Renci.iRods.Studio.Common.BaseClasses;
using System.Xml.Serialization;

namespace Renci.iRods.Studio.Models
{
    [Serializable]
    public class ConnectionProfile : ModelBase
    {
        [Required(ErrorMessage = "Profile name is required.")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Username is required.")]
        public string Username { get; set; }

        [Required(ErrorMessage = "Password is required.")]
        public string Password { get; set; }

        [Required(ErrorMessage = "Host is required.")]
        public string Host { get; set; }

        [Required(ErrorMessage = "Port is required.")]
        [Range(1, 65535, ErrorMessage = "Port is out of range.")]
        public uint Port { get; set; }

        public string ResourceName { get; set; }

        public string ZoneName { get; set; }

        public bool IsFavorite { get; set; }

        public ConnectionInfo ConnectionInfo
        {
            get
            {
                return new ConnectionInfo(this.Host, this.Port, this.Username, this.Password, this.ResourceName, this.ZoneName);
            }
        }

        /// <summary>
        /// Tests this connection.
        /// </summary>
        /// <returns></returns>
        public bool Test()
        {
            try
            {
                using (var connection = new IRodsConnection(this.ConnectionInfo))
                {
                    connection.Open();
                    connection.Close();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
